//
//  DetailViewController.swift
//  DataStructuresAlgorithms
//
//  Created by xiangzuhua on 2021/3/15.
//

import UIKit

class DetailViewController: UIViewController {
    var name: String?// 主题名字
    var inputText1: UITextField?
    var inputView1: UITextView?
    var doButton: UIButton!
    var inputView2: UITextView?
    var inputV1Placeholder: String?
    var inputV2Placeholder: String?
    
    let lead: CGFloat = 20.0
    var type: Int = 0
    
    private var graphData: Graph?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.navigationItem.backButtonTitle = "返回"
        // 分析类型
        getData()
        // 输入参数框
        if type == 1 {
            addInputView1()
        } else {
            addInputText1()
        }
        // 执行按钮
        addDoButton()
        // 打印结果框
        addInputView2()
        
        // 执行打印
        doPrintData()
        
        
        
        
        
        // Do any additional setup after loading the view.
    }
    func getData() {
        switch name {
        case LinkListReversal:
            type = 1
            
        case LinkListCycleDetection:
            type = 1
        case GraphBreadthFirst,GraphDepthFirst:
            type = 1
            let grap = Graph(8)
            grap.addEdige(bothway: 0, b: 1)
            grap.addEdige(bothway: 0, b: 3)
            grap.addEdige(bothway: 1, b: 2)
            grap.addEdige(bothway: 1, b: 4)
            grap.addEdige(bothway: 3, b: 4)
            grap.addEdige(bothway: 2, b: 5)
            grap.addEdige(bothway: 4, b: 5)
            grap.addEdige(bothway: 4, b: 6)
            grap.addEdige(bothway: 5, b: 7)
            grap.addEdige(bothway: 6, b: 7)
            graphData = grap
        case GraphTopoSortKahn, GraphTopoSortDFS:
            type = 1
            let grap = Graph(8)
            grap.addEdige(from: 0, to: 1)
            grap.addEdige(from: 0, to: 3)
            grap.addEdige(from: 1, to: 2)
            grap.addEdige(from: 1, to: 4)
            grap.addEdige(from: 3, to: 4)
            grap.addEdige(from: 2, to: 5)
            grap.addEdige(from: 4, to: 5)
            grap.addEdige(from: 4, to: 6)
            grap.addEdige(from: 5, to: 7)
            grap.addEdige(from: 6, to: 7)
            graphData = grap
        default:
            type = 0
        }
    }
    
    func addInputText1() {
        let top: CGFloat = isIPhoneX ? 88.0 + 20.0:20.0
        inputText1 = UITextField(frame: CGRect.init(x: lead, y: top, width: screenWidth - 2 * lead, height: 44))
        inputText1?.backgroundColor = UIColor.brown
        self.view.addSubview(inputText1!)
    }
    
    func addInputView1() {
        let top: CGFloat = isIPhoneX ? 88.0 + 20.0:20.0
        inputView1 = UITextView(frame: CGRect.init(x: lead, y: top, width: screenWidth - 2 * lead, height: 200))
        
        inputView1!.backgroundColor = UIColor.brown
        self.view.addSubview(inputView1!)
    }
    
    func addDoButton() {
        let top: CGFloat = (type == 1 ? inputView1!.frame.maxY : inputText1!.frame.maxY) + 40
        doButton = UIButton(type: .custom)
        doButton.frame = CGRect(x: lead, y: top, width: screenWidth - 2 * lead, height: 40)
        doButton.backgroundColor = UIColor.blue
        doButton.setTitle("执行", for: .normal)
        doButton.setTitleColor(.white, for: .normal)
        doButton.layer.cornerRadius = 5
        doButton.addTarget(self, action: #selector(doButtonAction(button:)), for: .touchUpInside)
        self.view.addSubview(doButton!)
    }
    
    func addInputView2() {
        let top: CGFloat = doButton!.frame.maxY + 40
        inputView2 = UITextView(frame: CGRect.init(x: lead, y: top, width: screenWidth - 2 * lead, height: 200))
        self.view.addSubview(inputView2!)
        inputView2!.backgroundColor = UIColor.cyan
    }
    
    // MARK:-action
    @objc func doButtonAction(button:UIButton) {
        print("do button action...")
    }
    
    func doPrintData() {
        switch name {
//        case LinkListReversal:
//
//        case LinkListCycleDetection:
        case GraphBreadthFirst:
            graphData?.bfs(0, 6)
        case GraphDepthFirst:
            graphData?.dfs(0, end: 6)
        case GraphTopoSortKahn:
            graphData?.topoSortByKahn()
        case GraphTopoSortDFS:
            graphData?.topoSortDFS()
            
        default:
            print("无")
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
